clear all
set more off


quiet do load_rreg2
local out2="\outregsQJE\"
local in2 ="\Data_and_Codes\"
cd "\workfiles"

use step001_QJE_2, clear
tsset firm_id time

*** define treatment as given inflation TIMES treatment status
gen Treat=treatment_old*infl

*** winsorize actual and expected price changes
winsor dprez, gen(dprez_w) p(0.02)

*-------------------------------------------------------------------------------
***				 collapse data to the annual frequency
*-------------------------------------------------------------------------------
foreach var in  dprez_w  sitimp5  occtot  sitgen  promig  liquid {
	gen L_`var'= `var'
}

	
collapse (mean) lnL ///
		(last) it12_all Treat treatment_old time ///
				L_dprez_w L_sitimp5 L_occtot L_sitgen L_promig L_liquid /// /*note: some of these variables are categorical */
				peso /// /* samping weight */
		 (last) setton3 /// /*industry */		
		(count) N_lnL= lnL N_it12_all=it12_all N_treatment_old=treatment_old, by(firm_id anno)


foreach var in L_sitimp5 L_occtot L_sitgen L_promig L_liquid setton3 anno {
	egen _`var'=group(`var')
	sum _`var'
	local rmax=r(max)
	forvalues i=2(1)`rmax' {
		gen _M_`var'_`i'=(_`var'==`i')
		gen _X_`var'_`i'=(_`var'==`i')*sqrt(peso)
	}
}
	
gen _W = sqrt(peso)
gen _Z = treatment_old*sqrt(peso)
gen _X_L_dprez_w=L_dprez_w*sqrt(peso)

drop if setton3==.

*-------------------------------------------------------------------------------
preserve
clear 

use "`in2'CGR_2006q1_2019q1_clean_ONLYCERVED.dta"
gen trimestre = quarter(dofq(time))
keep if trimestre == 1
drop trimestre
gen anno = yofd(dofq(time))
drop time
tsset firm_id anno
save "`in2'temp_ONLYCERVED.dta", replace
restore
*-------------------------------------------------------------------------------


merge 1:1 firm_id anno using "`in2'temp_ONLYCERVED.dta"

tsset firm_id anno
gen profit_share=c076/c051

tsset firm_id anno	

winsor profit_share, gen(profit_share_w) p(0.02)
	
replace profit_share_w=profit_share_w*100

***======================================================
*** 		include controls
***======================================================

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _Z _X* _W if anno>2012, nolog gen(w1)
replace w1=w1*peso

ivreghdfe f.profit_share_w treatment_old L_dprez_w _M* [aw=w1] if anno>2012, dkraay(2) absorb(i.setton3 i.anno)
outreg2 treatment_old using "`out2'Table05.dta", replace dta dec(3) ctitle("controls")


***======================================================
*** 	no controls but include year/industry dummies
***======================================================

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _Z _X_setton* _X_anno* _W if anno>2012, nolog gen(w1)
replace w1=w1*peso

ivreghdfe f.profit_share_w treatment_old [aw=w1] if anno>2012, dkraay(2) absorb(i.setton3 i.anno)
outreg2 treatment_old using "`out2'Table05.dta", append dta dec(3) ctitle("controls: year/indus")

***======================================================
*** 	no controls but include year/industry dummies
***======================================================
capture drop ones
gen ones=1

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _Z _W if anno>2012, nolog gen(w1)
replace w1=w1*peso

ivreghdfe f.profit_share_w treatment_old [aw=w1] if anno>2012, dkraay(2) absorb(ones)
outreg2 treatment_old using "`out2'Table05.dta", append dta dec(3) ctitle("no control")

***======================================================
*** 		include controls: pre-treatment
***======================================================

gen _treatment_oldX=treatment_old if anno==2013
egen treatment_oldX=min(_treatment_oldX), by(firm_id)	
	
tab treatment_oldX treatment_old, mis
tab treatment_oldX treatment_old if anno<2012, mis	
tab anno treatment_oldX, mis 


gen _Z1 = treatment_oldX*sqrt(peso) 

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _Z1 _X* _W if anno<=2011, nolog gen(w1)
replace w1=w1*peso

ivreghdfe f.profit_share_w treatment_oldX L_dprez_w _M* [aw=w1] if anno<=2011, dkraay(2) absorb(i.setton3 i.anno)
outreg2 treatment_oldX using "`out2'Table05.dta", append dta dec(3) ctitle("pre: controls")

***======================================================
*** 	no controls but include year/industry dummies
***======================================================

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _Z1 _X_setton* _X_anno* _W if anno<=2011, nolog gen(w1)
replace w1=w1*peso

ivreghdfe f.profit_share_w treatment_oldX [aw=w1] if  anno<=2011, dkraay(2) absorb(i.setton3 i.anno)
outreg2 treatment_oldX using "`out2'Table05.dta", append dta dec(3) ctitle("pre: year/indus")


***======================================================
*** 	no controls but include year/industry dummies
***======================================================
capture drop ones
gen ones=1

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _Z1 _W if anno<=2011, nolog gen(w1)
replace w1=w1*peso

ivreghdfe f.profit_share_w treatment_oldX [aw=w1] if anno<=2011, dkraay(2) absorb(ones)
outreg2 treatment_oldX using "`out2'Table05.dta", append dta dec(3) ctitle("pre: no control")


***======================================================
*** 	compute average profit 
***======================================================
capture drop ones
gen ones=1

capture drop _Y
gen _Y=f.profit_share_w *sqrt(peso)
capture drop w1
rreg2 _Y _W if anno>2012, nolog 

